
%% Correlation length

%After running April_22 to generate a 'Spins' matrix:

%Calculate AutoCorrelation function (CrossCorrelation with self):

AutoCor=real(ifft2(fft2(Spins).*conj(fft2(Spins))));

%Shift zero to center
ACS=fftshift(AutoCor);

figure
imshow(ACS,[],'InitialMagnification',400)

%Make X and Y array
N=size(AutoCor,1)
[X Y]=meshgrid((-N/2:N/2-1),(-N/2:N/2-1))

%Make R array
R=sqrt(X.^2+Y.^2)

figure
imshow(R,[],'InitialMagnification',400)

%Do radial averaging of Auto Correlation
RbinSize=1
h=figure

for rr=0:50   
    Rmask=(R<=(rr+RbinSize))&(R>rr);
    figure(h)
    imshow(Rmask,[],'InitialMagnification',400)
    pause(.1)
    Rave(rr+1)=mean(ACS(Rmask));
end

Rvec=(0:50);
figure
plot(Rvec,Rave)
xlabel('distance (Pixels)')
ylabel('correlation')

%Now we would fit that data to A*exp(-r/L) to get 'L'




